package com.he.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.he.constant.CommonConstant;
import com.he.constant.RedisConstant;
import com.he.domain.vo.*;
import com.he.entity.Article;
import com.he.enums.ArticleStatusEnum;
import com.he.mapper.*;
import com.he.service.ArticleService;
import com.he.service.BlogInfoService;
import com.he.service.RedisService;
import com.he.service.SiteConfigService;
import com.he.utils.IpUtils;
import com.he.utils.UserAgentUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author He
 * @version 1.0
 * @Date 2023/9/17 16:46
 * @Desc 博客信息业务实现类
 * */
@Service
public class BlogInfoServiceImpl implements BlogInfoService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private TagMapper tagMapper;

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private HttpServletRequest request;

    @Resource
    private RedisService redisService;

    @Resource
    private SiteConfigService siteConfigService;

    @Resource
    private ArticleService articleService;

    @Override
    public void addViewCount() {
        // 获取用户ip
        String ipAddress = IpUtils.getIpAddress(request);
        Map<String, String> userAgentMap = UserAgentUtils.parseOsAndBrowser(request.getHeader("User-Agent"));
        // 获取访问设备
        String browser = userAgentMap.get("browser");
        String os = userAgentMap.get("os");
        // 生成唯一用户标识
        String uuid = ipAddress + browser + os;
        String md5 = DigestUtils.md5DigestAsHex(uuid.getBytes());
        // 判断是否访问
        if (!redisService.hasSetValue(RedisConstant.SITE_VISITORS, md5)) {
            // 访问量+1
            redisService.incr(RedisConstant.SITE_VIEW_COUNT, 1);
            // 保存唯一标识
            redisService.setSet(RedisConstant.SITE_VISITORS, md5);
        }
    }

    @Override
    public BlogBackInfoVO getBlogBackInfo() {
        // 访问量
        Integer viewCount = redisService.getObject(RedisConstant.SITE_VIEW_COUNT);
        // 留言量
        Long messageCount = messageMapper.selectCount(null);
        // 用户量
        Long userCount = userMapper.selectCount(null);
        // 文章量
        Long articleCount = articleMapper.selectCount(new LambdaQueryWrapper<Article>()
                .eq(Article::getIsDelete, CommonConstant.FALSE));
        // 分类数据
        List<CategoryVO> categoryVOList = categoryMapper.getCategoryVOList();
        // 标签数据
        List<TagOptionVO> tagVOList = tagMapper.selectTagOptionList();
        // 查询用户浏览
        DateTime startTime = DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -7));
        DateTime endTime = DateUtil.endOfDay(new Date());
        //TODO: 2023/9/17 16:49
        //List<UserViewVO> userViewVOList = visitLogMapper.selectUserViewList(startTime, endTime);
        // 文章统计
        List<ArticleCountDateVO> articleCountDateVOS = articleMapper.selectArticleCountAndDate(DateTime.now().dayOfYear());
        // 文章排行
        List<ArticleHotVO> hotArticleList = articleService.getHotArticleList();
        BlogBackInfoVO blogBackInfoVO = BlogBackInfoVO.builder()
                .articleStatisticsList(articleCountDateVOS)
                .tagVOList(tagVOList)
                .viewCount(viewCount)
                .messageCount(messageCount)
                .userCount(userCount)
                .articleCount(articleCount)
                .categoryVOList(categoryVOList)
                .articleHotVOList(hotArticleList)
//                .userViewVOList(userViewVOList)
                .build();
        return blogBackInfoVO;
    }

    @Override
    public BlogInfoVO getBlogInfo() {
        //查询文章数量
        Long articleCount = articleMapper.selectCount(new LambdaQueryWrapper<Article>()
                .eq(Article::getStatus, ArticleStatusEnum.PUBLIC.getStatus())
                .eq(Article::getIsDelete, CommonConstant.FALSE));
        //查询分类数量
        Long categoryCount = categoryMapper.selectCount(null);
        //查询标签数量
        Long tagCount = tagMapper.selectCount(null);
        //查询访问量
        Integer count = redisService.getObject(RedisConstant.SITE_VIEW_COUNT);
        String viewCount = Optional.ofNullable(count).orElse(0).toString();
        //获取网站配置
        SiteConfigVO siteConfig = siteConfigService.getSiteConfig();
        //封装数据
        return BlogInfoVO.builder()
                .articleCount(articleCount)
                .categoryCount(categoryCount)
                .tagCount(tagCount)
                .viewCount(viewCount)
                .siteConfig(siteConfig)
                .build();
    }

}
